/**
 * @author hxd
 * 我在公司内部“东西”上查到一个，但不能确定是不是符合标准，你参考吧！配电盘上的模拟线应按电压等级涂不同颜色。
 * 电压由高至低应按淡黄、紫、朱红、鲜黄、绛红、深兰等顺序（500KV、220KV、110KV、35KV、10KV、6KV）。
 */
var nomalColor = "#ff0000";
var hoverColor = "#0000ff";
var selectColor = "#00ff00";

function hover_in() {
  //var oldwidth=this.attr("stroke-width");
  //this.attr({"stroke-width":oldwidth+2});
  this.attr({
    "stroke": hoverColor
  });
}

function hover_out() {
  this.attr({
    "stroke": nomalColor
  });
}

function click_select() {
  this.attr({
    "stroke": selectColor
  });
  // var resize = this.paper.el("rect").attr(this.getBBox());
  // resize.drag(resizeing,begin_resize,end_resize);

}

function begin_resize(x, y, event) {
  this.newtrans = this.transform().localMatrix;
}

function resizeing(dx, dy, x, y, event) {


  var path = ["M", x, y, "l", dy, 0, "z"].join(" ");
  // this.path(path);
  // this.attr("path",path);
  this.attr({
    x: x,
    y: y
  });
}

function end_resize(event) {

}

function dbclick_edit() {
  alert("");
}
//初始化matrix
function drag_start(x, y, target) {

  this.newtrans = this.transform().localMatrix;
};
//设置只有x移动的matrix
function drag_move(dx, dy, x, y, target) {

  this.trans = new Snap.Matrix();
  this.trans.translate(dx, 0);
  var node = this.node;
  //通过遍历父类，找到最上层的 g块元素，作为移动的对象
  while (node.parentNode.nodeName == "g") {
    node = node.parentNode;
  }
  this.node = node;

  this.trans.add(this.newtrans);

  this.transform(this.trans);
};

function drag_stop(target) {
  //alert(target);

}
//====================母线
Snap.plugin(function(Snap, Element, Paper, global) {
  var proto = Paper.prototype,
    is = Snap.is,
    elproto = Element.prototype;

  Snap.busLine = function() {
    alert("Snap");
  };
  Element.prototype.busLine = function() {
    alert("Element");
  };
  proto.busLine = function(x, y, length, values, showName) {
    //  this.circle(30, 30, 30); // is the same as...
    length = length || 800;
    //默认的length长度为800
    var path = ["M", x, y, "l", length * 0.9, 0, "z"].join(" ");
    var p = this.path(path).attr(Snap.avc.pathattr);

    p.click(click_select);
    p.dblclick(dbclick_edit);
    p.hover(hover_in, hover_out);

    var bb = p.getBBox();
    var text = this.text(x + bb.width / 2, y - bb.height / 2 - Snap.avc.strokeWidth / 2 - Snap.avc.textmargin, values).attr(Snap.avc.textattr);
    var g = this.g();
    g.add(p, text);
    g.drag(resizeing, begin_resize, end_resize);
    // return new Element(g);
    return p; //返回母线，不包括文字，在组合的时候会用到母线的边框
  };
  proto.transformer = function(x, y, values) {
    //a rx ry xr large sweep x y
    // rx ry 半径
    // xr  X轴水平方向的夹角
    // large  大小弧
    // sweep  圆心在起点的顺时针反向 还是逆时针方向  1 顺时针
    // x y  终点 
    p = this.path().attr(Snap.avc.pathattr);
    p.attr({
      path: ["M", x, y, "l 0 20 a15 15 0 1 0 15 15 15 15 1 0 0 -15 -15 m 0 50 a15 15 0 1 1 15 -15  15 15 0 0 1  -15 15   l 0 20 z"].join(" ")
    });
    //   p.attr({
    //        path : ["M", x, y, "l 0 20 a15 15 0 1 0 15 15 15 15 1 0 0 -15 -15 m 0 20 a15 15 0 1 0 15 15 15 15 1 0 0 -15 -15 m 0 30 l 0 20 z"].join(" ")
    //    });
    p.click(click_select);
    p.dblclick(dbclick_edit);
    p.hover(hover_in, hover_out);
    var bb = p.getBBox();

    // var rect =this.rect(x,y,bb.width,bb.height);

    text = this.text(x + bb.width / 2 + Snap.avc.strokeWidth / 2, y + bb.height / 2, values).attr(Snap.avc.textattr);
    var g = this.g();
    g.add(p, text);
    return g;
  };

  proto.three_transformer = function(x, y, values) {

    var p = this.path().attr(Snap.avc.pathattr);

    p.attr({
      path: ["M", x, y, "l 0 20 a15 15 0 1 0 15 15 15 15 1 0 0 -15 -15   m -10 10 a15 15 0 1 0 15 15 15 15 1 0 0 -15 -15  m 20 0 a15 15 0 1 0 15 15 15 15 1 0 0 -15 -15  m 15 20 l 10 10 0 30  m -60 -40 l -10 10 0 30 "].join(" ")
    });
    p.click(click_select);
    p.dblclick(dbclick_edit);
    p.hover(hover_in, hover_out);
    var bb = p.getBBox();

    // var rect =this.rect(x,y,bb.width,bb.height);

    text = this.text(x + bb.width / 2 + Snap.avc.strokeWidth / 2, y + bb.height / 2, values).attr(Snap.avc.textattr);
    var g = this.g();
    g.add(p, text);
    return g;
  };

  //proto.three_transformer={};
  // proto.three_transformer.getBox=function(){
  //   alert("OK");
  //    return this.getBBox();
  // };
  proto.breaker = function(x, y, name) {

    var p = this.path().attr({
      stroke: nomalColor,
      "stroke-width": 5,
      id: "breaker",
      title: "asdf",
      x: 0,
      y: 0
    });
    //解决fireffox 移动 到线段中间进行绘制的时候出现，由于线条宽度导致移动位置偏差线宽的bug
    //最好 m的时候不要移动到线段的中间位置，不然会出现由于线条宽度照成的误差
    p.attr({
      path: ["M", x, y, "l 0 20 ,5 0 ,0 30, -5 0 m 0 -30 l -5 0 0 30 5 0 0 20"].join(" ")
    }).attr("id", "ffff");
    //     p.attr({
    //         path : ["M", x, y, "l 0 20 ,5 0 ,0 30, -10 0 ,0 -30, 5 0 m -5 0 m 5 30 l0 20 z"].join(" ")
    //     }).attr("id","ffff");
    p.click(click_select);
    p.dblclick(dbclick_edit);
    p.hover(hover_in, hover_out);
    //在开关旁边显示 开关名称
    var bb = p.getBBox();
    var text = this.text(bb.width / 2 + Snap.avc.strokeWidth + 5 + x, y + bb.height / 2, name).attr(Snap.avc.textattr);
    var g = this.g();
    g.add(p, text);
    g.drag(drag_move, drag_start, drag_stop);
    return g;
  };
  proto.transformer_withBreaker = function(x, y, values) {


    var breaker1 = this.breaker(x, y, '1');

    var bb1 = breaker1[0].getBBox();
    var x1 = x,
      y1 = y + bb1.height;
    var transf = this.transformer(x1, y1, values);

    var bb2 = transf[0].getBBox();
    var x2 = x1,
      y2 = y1 + bb2.height;
    var breaker2 = this.breaker(x2, y2, '111111111111111111111');
    var g = this.g();
    g.add(breaker1, transf, breaker2);
    // g.drag();
    //   g.drag(drag_move,drag_start,drag_stop);
    g.click(click_select);
    g.dblclick(dbclick_edit);
    g.hover(hover_in, hover_out);
    return g;
  };
  proto.three_transformer_withBreaker = function(x, y, values) {


    var breaker1 = this.breaker(x, y, '1');

    var bb1 = breaker1[0].getBBox();
    var x1 = x,
      y1 = y + bb1.height;
    var transf = this.three_transformer(x1, y1, values);

    var bb2 = transf[0].getBBox();
    var x2 = x1 - bb2.width / 2,
      y2 = y1 + bb2.height;
    var breaker2 = this.breaker(x2, y2, '111111111');
    var x3 = x1 + bb2.width / 2,
      y3 = y1 + bb2.height;
    var breaker3 = this.breaker(x3, y3, '1');

    var g = this.g();
    g.add(breaker1, transf, breaker2, breaker3);
    g.drag(drag_move, drag_start, drag_stop);
    return g;
  };

});
//===========普通变压器
/*
Raphael.fn.transformer = function (x, y, values) {
var set = this.set(),p;
p = this.path().attr(Raphael.avc.pathattr);

p.attr({
path: [
"M", x, y,
"l 0 20 a15 15 0 1 0 15 15 15 15 1 0 0 -15 -15 m 0 20 a15 15 0 1 0 15 15 15 15 1 0 0 -15 -15 m 0 30 l 0 20 z"
].join(",")
});
p.click(click_select);
p.dblclick(dbclick_edit);
p.hover(hover_in,hover_out);
var bb=p.getBBox();

// var rect =this.rect(x,y,bb.width,bb.height);

text = this.text(x+bb.width/2+Raphael.avc.stroke_width/2,y+bb.height/2 , values).attr(Raphael.avc.textattr);

return set.push(p);
};
Raphael.fn.transformer_withBreaker = function (x, y, values) {
var set = this.set();

var breaker1=this.breaker(x,y,'1');

var bb1 = breaker1.getBBox();
var x1=x,y1=y+bb1.height;
var transf=this.transformer(x1,y1,values);

var bb2=transf.getBBox();
var x2=x1,y2=y1+bb2.height;
var breaker2=this.breaker(x2,y2,'1');
//var bb3=breaker2.getBBox();

// var rect =this.rect(x1-bb2.width/2-Raphael.avc.stroke_width/2,y1,bb2.width+Raphael.avc.stroke_width,bb2.height);

set.push(breaker1,transf,breaker2);
var bb3=set.getBBox();

//var rect =this.rect(x-bb3.width/2,y,bb3.width,bb3.height);

return set;
};

//===========
//=========== 断路器
Raphael.el.breaker = function (x, y) {
var paper = this.paper || this[0].paper;
p = paper.path().attr({  stroke: nomalColor,"stroke-width":5});
p.attr({
path: [
"M", x, y,
"l 0 20 ,5 0 0 30 -10 0 0 -30 5 0 m 0 30 0 20 z"
].join(",")
});
p.click(click_select);
p.dblclick(dbclick_edit);
p.hover(hover_in,hover_out);
return p.insertBefore(this.node ? this : this[0]);
};
Raphael.fn.breaker = function (x, y, name) {

var set = this.set();
p = this.path().attr({  stroke: nomalColor,"stroke-width":5 ,id:"breaker",title:"asdf"});
p.attr({
path: [
"M", x, y,
"l 0 20 ,5 0 0 30 -10 0 0 -30 5 0 m 0 30 0 20 z"
].join(",")
});
p.click(click_select);
p.dblclick(dbclick_edit);
p.hover(hover_in,hover_out);
//在开关旁边显示 开关名称
var bb = p.getBBox();
var text = this.text(bb.width/2+Raphael.avc.stroke_width+5+x, y+bb.height/2, name).attr(Raphael.avc.textattr);
//var rect =this.rect(x-bb.width/2-Raphael.avc.stroke_width/2,y,bb.width+Raphael.avc.stroke_width,bb.height);
return set.push(p,text);
};
*/
// 公共属性
Snap.avc = {
  textattr: {
    font: '14px Arial, sans-serif',
    fill: '#000',
    "text-anchor": "start",
    href: "tttt"
  },

  strokeWidth: 5,
  textmargin: 10,
  nomalColor: "#ff0000",
  hoverColor: "#0000ff",
  selectColor: "#00ff00",
  pathattr: {
    stroke: "#ff0000",
    strokeWidth: 5,
    fill: '#fff',
  }
};
/*
 //自定义属性，通过title来获取指定的item,href,在点击的时候会跳转到对应界面。
 Raphael.fn.getItemByTitle=function(title){

 var bot = this.bottom;
 while (bot) {
 if (bot.attr("title") == title) {
 return bot;
 }
 //alert(bot.id);
 //bot.attr({fill:"#00ff00"});
 bot = bot.next;
 }
 return null;
 };
 */